/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_dotnet_remoting_detect.c                         */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/30 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <dotnet_remoting/dotnet_remoting_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/dotnet_remoting/config.h"

extern int dotnet_remoting_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    i_ret = dotnet_remoting_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x15\x12\x00\x00\x00\x12\x07\x67\x65\x74\x4e\x61\x6d\x65\x12\x5f\x52\x65\x6d\x6f\x74\x69\x6e\x67\x4f\x62\x6a\x65\x63\x74\x73\x2e\x49\x50\x65\x72\x73\x6f\x6e\x2c\x20\x52\x65\x6d\x6f\x74\x69\x6e\x67\x4f\x62\x6a\x65\x63\x74\x73\x2c\x20\x56\x65\x72\x73\x69\x6f\x6e\x3d\x31\x2e\x30\x2e\x30\x2e\x30\x2c\x20\x43\x75\x6c\x74\x75\x72\x65\x3d\x6e\x65\x75\x74\x72\x61\x6c\x2c\x20\x50\x75\x62\x6c\x69\x63\x4b\x65\x79\x54\x6f\x6b\x65\x6e\x3d\x6e\x75\x6c\x6c\x01\x00\x00\x00\x12\x08\x78\x69\x61\x6f\x6d\x69\x6e\x67\x0b";
    p_st_packet->ui_payload_len = 143;

    i_ret = dotnet_remoting_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case3(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x2e\x4e\x45\x54\x01\x00\x02\x00\x00\x00\x21\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x16\x11\x08\x00\x00\x12\x08\x78\x69\x61\x6f\x6d\x69\x6e\x67\x0b";
    p_st_packet->ui_payload_len = 49;

    i_ret = dotnet_remoting_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case4(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x2e\x4e\x45\x54\x01\x00\x00\x00\x00\x00\x8f\x00\x00\x00\x04\x00\x01\x01\x2f\x00\x00\x00\x74\x63\x70\x3a\x2f\x2f\x31\x39\x32\x2e\x31\x36\x38\x2e\x32\x34\x2e\x31\x33\x31\x3a\x38\x30\x38\x30\x2f\x52\x65\x6d\x6f\x74\x69\x6e\x67\x50\x65\x72\x73\x6f\x6e\x53\x65\x72\x76\x69\x63\x65\x06\x00\x01\x01\x18\x00\x00\x00\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x2f\x6f\x63\x74\x65\x74\x2d\x73\x74\x72\x65\x61\x6d\x00\x00";
    p_st_packet->ui_payload_len = 0;

    i_ret = dotnet_remoting_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case5(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x2e\x4e\x45\x54\x01\x00\x00\x00\x00\x00\x8f\x00\x00\x00\x04\x00\x01\x01\x2f\x00\x00\x00\x74\x63\x70\x3a\x2f\x2f\x31\x39\x32\x2e\x31\x36\x38\x2e\x32\x34\x2e\x31\x33\x31\x3a\x38\x30\x38\x30\x2f\x52\x65\x6d\x6f\x74\x69\x6e\x67\x50\x65\x72\x73\x6f\x6e\x53\x65\x72\x76\x69\x63\x65\x06\x00\x01\x01\x18\x00\x00\x00\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x2f\x6f\x63\x74\x65\x74\x2d\x73\x74\x72\x65\x61\x6d\x00\x00";
    p_st_packet->ui_payload_len = 103;

    i_ret = dotnet_remoting_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

int add_suite_dotnet_remoting_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" dotnet_remoting_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
